$().ready(function(){
	
//	Cufon.replace("h1");

    $baseUrl=$("#baseUrl").text();
    
    /* Method for checking if the email is taken for #email field*/
    $.validator.addMethod("emailTaken", function(value) {
    	var $returnValue;
    	
    	$.ajax({
    		  url: $baseUrl+ '/user/checkemailajax',
    		  type: 'post',
    		  async: false,
    		  data: ({email : value}),
    		  success: function(data) {
	 		  $returnValue = (data!=1);
	 		   return;
    		  }});   	
    	
    	return $returnValue;
	 	
    }, '');
    

    /* ajax checking for login form */
    $("#loginForm").submit(function(){
    	var $returnValue = true;
    	
    	$.ajax({
    		  url: $baseUrl+ '/user/loginajax',
    		  type: 'post',
    		  async: false,
    		  data: ({email : $("#email").val(), password : $("#password").val()}),
    		  success: function(data) {
    				$("#password-element div.error").remove();
    			if (data!=1) {
    				$returnValue = false;
    				$("#password-element").append("<div class='error'>邮箱和密码配不起来呀。。。再试试？</div>");
    			}
    			return;
    		  }});   	
    	
    	return $returnValue;
    	
    });
    	
    
    $("#registerForm").validate({
    	rules: {
		username: {
			required: true,
			minlength: 2,
			maxlength: 20
		},
		password: {
			required: true,
			minlength: 6,
			maxlength: 20
		},
		confirm: {
			required: true,
			minlength: 6,
			maxlength: 20,
			equalTo: "#password"
		},
		email: {
			required: true,
			email: true,
			emailTaken: true
		}
	},
	messages: {
		username: {
			required: "您的真名不可以为空哦！",
			minlength: "您的真名这么短呀。。。至少要两个字哦！",
			maxlength: "这。。这。。您的真名？来个20字以下的简称吧。。。"
		},
		password: {
			required: "密码不可以为空哦！",
			minlength: "密码太短很危险的，至少6个字吧",
			maxlength: "。。。好长。。。您真谨慎。。。20个字以下吧。。"
		},
		confirm: {
			required: "再输一遍密码嘛！",
			minlength: "密码太短很危险的，至少6个字吧",
			maxlength: "。。。好长。。。您真谨慎。。。20个字以下吧。。",
			equalTo: "密码两次输入好像不一样耶。。。-_-||"
		},
		email:{
			required: "电子邮箱不能为空哦！",
			email: "电子邮箱的格式好像不对哦。。。再检查检查？",
			emailTaken: "这个邮箱已经被别人注册了。。。"
		} 
	}
    });
         
    $("#loginForm").validate({
    	rules: {
		
		password: {
			required: true
		},
		email: {
			required: true,
			email: true
		}
	},
	messages: {
		password: "密码不可以为空哦！",
		email: "电子邮箱的格式好像不对哦。。。再检查检查？"
	}
    });
    
    
    
    
});